package yw.substart.example.controller;

import com.alibaba.fastjson.JSON;
import org.springframework.web.bind.annotation.ModelAttribute;
import yw.substart.example.util.IpUtil;
import yw.substart.example.util.MessageUtil;

import javax.servlet.http.HttpServletRequest;

/**
 * @author lwq 2018/5/21
 * @comment
 */
public class BaseController {

    public JSON success() {
        return MessageUtil.defaultReturnMessage(true);
    }

    public JSON success(Object object) {
        return MessageUtil.defaultReturnMessage(true, object);
    }

    public JSON error() {
        return MessageUtil.defaultReturnMessage(false);
    }

    public JSON error(Object object) {
        return MessageUtil.defaultReturnMessage(false, object);
    }

    private static final ThreadLocal<HttpServletRequest> requestLocal = new ThreadLocal<HttpServletRequest>();

    @ModelAttribute
    public void setRequest(HttpServletRequest rquest) {
        requestLocal.remove();
        requestLocal.set(rquest);
    }

    public String getIp() {
        return IpUtil.getRemoteRealIP(requestLocal.get());
    }

}
